package ModelLayer;
import java.util.ArrayList;
//The purpose of using an ArrayList is to store the DVD instances in the Register.
public class DvdRegister
{
    private ArrayList<DVD> dvDcollection;
    private static DvdRegister instance;
   
    /** CONSTRUCTORS */
    public DvdRegister()
    {
        dvDcollection = new ArrayList<DVD>();
    }
    
    /** ACCESSORS */
    public static DvdRegister getInstance()
    {
        if(instance == null)
        {
            instance = new DvdRegister();
        }
        return instance;
    }

    public ArrayList<DVD> findDVD()
    //Shows our DVD collection.
    {
        return dvDcollection;
    }
    public DVD getDVD(String dvd_id)
    //Finds a particular DVD in our collection.
    {
        for(int index = 0 ; index < dvDcollection.size(); index++)
        {
            if(dvDcollection.get(index).getDVDId().equals(dvd_id))
            {
                return dvDcollection.get(index);
            }
        }
        return null;
    }
    
    /** MUTATORS */
        public void addDVD(String dvd_id, String title, String artist, String publicationDate)
    //Add a new DVD to our collection.
    {
        dvDcollection.add(new DVD(dvd_id, title, artist, publicationDate));
    }
    public void deleteDVD(String dvd_id)
    //Delete a DVD from our collection.
    {
        int index = dvDcollection.indexOf(getDVD(dvd_id));
        dvDcollection.remove(index);
    }
    public void updateDVD(DVD myDVD, String dvd_id, String title, String artist, String publicationDate)
    //Updates details of a DVD in our collection.
    {
        myDVD.setId(dvd_id);
        myDVD.setTitle(title);
        myDVD.setArtist(artist);
        myDVD.setPublicationDate(publicationDate);
    }
    public DVD findDVD(String title){
        int i = 0;
        boolean found = false;
        while(!found&&dvDcollection.size()>i){
            if(title.equals(dvDcollection.get(i).getTitle())){
                found = true; 
            }
            else{
                i++;
            }
        }
        if(!found){
            return null;
        }
        return dvDcollection.get(i);
    }
}